layui.use(['jquery', 'table'], function () {
    var table = layui.table;
    var $ = layui.$;
    var form = layui.form;

    mytable = table.render({
        elem: '#jijin-table'
        , url: 'http://localhost:8025/list'
        , cols: [[
            {field: 'code', title: '代码', align: 'center', width: 90}
            , {field: 'name', title: '名称', align: 'center'}
            , {field: 'gszzl', title: '今天涨幅', align: 'center', width: 90}
            , {field: 'gsz', title: '今日估值', align: 'center', width: 90}
            , {field: 'estimatePercent', title: '收益率', align: 'center', width: 90}
            , {field: 'estimate', title: '收益估算', align: 'center', width: 90}
            , {fixed: 'right', title: '操作', toolbar: '#barDemo', align: 'center', width: 120}
        ]]
        , done: function (res, curr, count) {
            $("tr:gt(0) td:nth-child(6) div").each(function (i, j) {
                if ($(this).html().substr(0, 1) == "-") {
                    $(this).css("background-color", "#58D68D");
                } else {
                    $(this).css("background-color", "#E74C3C");
                }
            });
        }
    });

    //添加
    $("#save-btn").on("click", function () {
        $.ajax({
            url: "http://localhost:8025/create",
            type: "post",
            data: {"code": $("#code").val()},
            dataType: "json",
            success: function (result) {
                mytable.reload();
            }
        })
    });

    //分析
    $("#analyze-btn").on("click", function () {
        $.ajax({
            url: "http://localhost:8025/analyze",
            type: "get",
            dataType: "json",
            success: function (result) {
                mytable.reload();
            }
        })
    });


    //删除
    table.on('tool(table)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.msg('确定删除吗？', {
                time: 20000, //20s后自动关闭
                btn: ['确定', '取消']
                , yes: function (index, layero) {
                    $.ajax({
                        url: "http://localhost:8025/delete",
                        type: "post",
                        data: {"code": $(data.code).html()},
                        dataType: "json",
                        success: function (result) {
                            obj.del();
                            layer.close(index);
                        }
                    });
                }
            });
        } else if (obj.event === 'edit') {
            $("#code-input").val($(data.code).html());
            layer.open({
                type: 1,
                closeBtn: 0,
                shadeClose: true,
                content: $("#update")
            });
        }
    });

    //编辑
    form.on('submit(submit-btn)', function (data) {
        var data = JSON.stringify(data.field);
        data.code = $("#code-input").val();

        $.ajax({
            url: "http://localhost:8025/edit",
            type: "post",
            async: false,
            data: $(".layui-form").serialize(),
            dataType: "json",
            success: function (result) {
                mytable.reload();
            }
        });
        document.getElementById("edit-from").reset();
        layer.closeAll();
    });


    //自动刷新
    // setInterval(function () {
    //     mytable.reload();
    // }, 30000);

});
